<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="./css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="css/submitMeter.css" />
	</head>

	<body>
		<!-- 查询某一户 -->
		<div class="user-inquery">
			<div class="mui-input-row mui-plus-visible user-inquery-input">
				<input class="mui-input mui-input-clear" type="number" name="" id="userNum" value="" />
			</div>
			<div class="user-inquery-btn">
				<button id="userInquery" type="button" class="mui-btn mui-btn-blue">查询</button>
			</div>
			<!-- 提交 -->
			<div class="submit">
				<button id="subm" type="button" class="mui-btn mui-btn-blue">提交</button>
			</div>
		</div>

		<!-- 抄表记录列表 -->
		<div class="mui-input-row mui-checkbox mui-left">
			<label>全选</label>
			<input class="qx" type="checkbox" name="" id="qx" value="" />
		</div>
		<div id="userList">
			
		</div>

		<script src="./js/mui.min.js"></script>
		<script src="js/jquery-2.1.1.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/Mobile.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/app.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			// mui.init()

			// 判断token是否过期
			// var tokenStatus = checkToken(glbToken).status;
			// 全选按钮
			$("#qx").click(function() {
				var x = $("#qx").prop('checked');
				// $(".noCheck").prop('checked',x);
				$(".noCheck").each(function() {
					//this表示每次遍历到的标签
					$(this).prop('checked', x);
				})
			})

			// 查询用户
			mui('.user-inquery-btn').on('tap', '#userInquery', function() {
				var myStorage = JSON.parse(localStorage.getItem("users"));
				var glbToken = myStorage.glbToken;
				if (checkToken(glbToken).status == "ok") {
					var userId = $("#userNum").val();
					var meterInfoArr = getMeter(userId, glbToken).rows;
					if (meterInfoArr.length == 0) {
						alert('无记录');
						return false
					}
					if (userId == '') {
						var list = "";
						var WRDIDArr = [];
						var userList = document.getElementById('userList');
						for (var i = 0; i < meterInfoArr.length; i++) {
							list += "<div class='mui-input-row mui-checkbox mui-left list-center'>" +
								"<label>" +
								"<div class=''>用户名：" + meterInfoArr[i].UserName + "</div>" +
								"<div class=''>用户号：" + meterInfoArr[i].UserID + "</div>" +
								"<div class=''>水表号：" + meterInfoArr[i].WaterID + "</div>" +
								"<div class=''>本次抄码：" + meterInfoArr[i].ThisMonthCode + "吨</div>" +
								"</label>" +
								"<input class='noCheck' name='meterInfo' type='checkbox' value='"+ meterInfoArr[i].WRDID +"' />" +
								"</div>";
						}
							userList.innerHTML = list;
					} else {
						if (meterInfoArr.length == 0) {
							alert('该用户已提交或用户号不正确');
							return false
						}
						var list = "";
						var userList = document.getElementById('userList');
						for (var i = 0; i < meterInfoArr.length; i++) {
							list += "<div class='mui-input-row mui-checkbox mui-left list-center'>" +
								"<label>" +
								"<div class=''>用户名：" + meterInfoArr[i].UserName + "</div>" +
								"<div class=''>用户号：" + meterInfoArr[i].UserID + "</div>" +
								"<div class=''>水表号：" + meterInfoArr[i].WaterID + "</div>" +
								"<div class=''>本次抄码：" + meterInfoArr[i].ThisMonthCode + "吨</div>" +
								"</label>" +
								"<input class='noCheck' name='meterInfo' type='checkbox' value='"+ meterInfoArr[i].WRDID +"' />" +
								"</div>";
						}
						userList.innerHTML = list
					}
				}else{
					app.setState({});
					plus.webview.getLaunchWebview().show("pop-in");
					setTimeout(function(){
						alert("此账号已在其他设备登录,请重新登录!");
						localStorage.clear();
						plus.runtime.restart();
					},30);
				}
			})

			// 提交抄表
			mui('.submit').on('tap', '#subm', function() {
				var myStorage = JSON.parse(localStorage.getItem("users"));
				var glbToken = myStorage.glbToken;
				if (checkToken(glbToken).status == "ok") {
					var btnArray = ['是', '否'];
					mui.confirm('请确认抄表信息是否正确？', '金财抄表', btnArray, function(e) {
						if (e.index == 0) {
							var submitStr = "";
							$.each($('input:checkbox[name=meterInfo]:checked'), function() {
								submitStr += $(this).val() + ',';
							});
							submitStr = submitStr.substr(0, submitStr.length - 1);
							if (submitStr.substr(0, 1) == ',') {
								submitStr = submitStr.substr(1)
							}
							var submitData = submitMeter(submitStr, glbToken);
							// console.log(submitStr);
							if (submitData.status == 'ok') {
								mui.toast(submitData.msg,{ duration:'short', type:'div' });
								$('input:checkbox[name=meterInfo]:checked').parent().remove();
								$('#qx').prop('checked', false);
							} else if(submitData.msg == '请选择要提交的项！'){
								alert('请选择要提交的项');
							}else{
								mui.toast(submitData.msg,{ duration:'short', type:'div' });
							}
						} else {
							return
						}
					})
				}else {
					app.setState({});
					plus.webview.getLaunchWebview().show("pop-in");
					setTimeout(function(){
						alert("此账号已在其他设备登录,请重新登录!");
						localStorage.clear();
						plus.runtime.restart();
					},30);
				}
			})
		</script>
	</body>

</html>
